package com.bizdk.fastfx.controller;

import com.bizdk.fastfx.fxsupport.FxmlView;
import com.bizdk.fastfx.fxsupport.JavaFxController;
import com.bizdk.fastfx.model.DoctorModel;
import com.bizdk.fastfx.service.DoctorService;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.stage.Modality;
import javafx.stage.Stage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;

import java.net.URL;
import java.util.ResourceBundle;

@Controller
@FxmlView("/view/AddDoctorStage.fxml")
public class AddDoctorStageController extends JavaFxController {
    @FXML
    private TextField tf_id;

    @FXML
    private TextField tf_name;

    @FXML
    private PasswordField pf_passwd;

    @FXML
    private ComboBox<String> cb_title;

    @Autowired
    private DoctorService doctorService;

    @Autowired
    private MainStageController mainStageController;

    private DoctorModel model;


    @Override
    public void initialize(URL location, ResourceBundle resources) {
        getStage().setResizable(false);
        cb_title.getItems().addAll("医士", "医师", "住院医师", "主治医师", "副主任医师", "主任医师");
    }

    public void handlerSave(ActionEvent event) {
        event.consume();
        if (null == model) {
            model = new DoctorModel();
        }
        model.setName(tf_name.getText());
        model.setPasswd(pf_passwd.getText());
        model.setTitle(cb_title.getValue());
        if (StringUtils.isEmpty(model.getId())) {
            doctorService.addModel(model);
        } else {
            doctorService.updateModel(model);
        }
        mainStageController.getDoctorList();
        getStage().close();
    }

    public void handlerReset(ActionEvent event) {
        event.consume();
        reset();
    }

    public void show(DoctorModel model) {
        this.model = model;
        Stage stage = getStage();
        if (null == stage) {
            stage = new Stage();
            stage.initOwner(mainStageController.getStage()); //设置两个窗口关联，s2窗口的拥有者为s1
            stage.initModality(Modality.WINDOW_MODAL); //s2设置模块化，对s1窗口起作用
            setStage(stage);
        }
        Scene scene = getScene();
        if (null == scene) {
            scene = new Scene(getRoot());
            setScene(scene);
        } else {
            scene.setRoot(getRoot());
        }
        stage.setScene(scene);
        stage.centerOnScreen();
        stage.show();

        if (null == model.getId()) {
            getStage().setTitle("新增医生");
        } else {
            getStage().setTitle("编辑医生");
        }

        reset();
    }

    private void reset() {
        tf_id.setText(model.getId());
        tf_name.setText(model.getName());
        pf_passwd.setText(model.getPasswd());
        cb_title.getSelectionModel().select(model.getTitle());
    }
}
